package org.millioncall.yueyoga.admin.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.service.HallService;
import org.millioncall.yueyoga.admin.service.StatisticsService;
import org.millioncall.yueyoga.admin.service.UserService;
import org.millioncall.yueyoga.admin.service.stat.GroupByTableBuilder;
import org.millioncall.yueyoga.admin.service.stat.TableView;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;

/**
 * title 统计功能
 * 
 * @author lisj 2014年6月13日 上午10:05:54
 */
@Controller
public class StatisticsAction extends BaseAction {

	// --统计查询
	private StatisticsService statisticsService;
	private UserService userService;
	private HallService hallService;

	public void setStatisticsService(StatisticsService statisticsService) {
		this.statisticsService = statisticsService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setHallService(HallService hallService) {
		this.hallService = hallService;
	}

	/************************* 进入统计页面 *****************************/

	/*
	 * 进入信息统计页面
	 */
	@RequestMapping(value = "/toMemberStatistics.jhtml")
	public String toMemberStatistics(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		return "statistics_info";
	}

	/****************************************************************/

	/**************************** 统计执行 *******************************/

	/*
	 * 会员统计
	 */
	@RequestMapping(value = "/memberStatistics.jhtml")
	public void memberStatistics(String ltGroup, String tpGroup, String statFun,
			String statCol, String criteria, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		String json = "";
		// --左侧分组条件
		List<String> leftGroupBy = getGroups(ltGroup);

		// --头部分组条件
		List<String> topGroupBy = getGroups(tpGroup);

		// --统计会员结果
		List<Object[]> results = statisticsService.memberStatQueryByCdts(leftGroupBy,
				topGroupBy, statCol, statFun, criteria);
		if (results == null || results.isEmpty()) {
			json = "";
		} else {
			GroupByTableBuilder gbc = new GroupByTableBuilder(results,
					leftGroupBy.size(), topGroupBy.size());
			TableView tv = gbc.buildTable();
			json = JSON.toJSONString(tv);
		}
		WebAppUtils.ajaxResponse(response, json);
	}

	/*
	 * 班级统计
	 */
	@RequestMapping(value = "/clazzStatistics.jhtml")
	public void clazzStatistics(String ltGroup, String tpGroup, String statFun,
			String statCol, String criteria, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		// --获取班级[bj]的统计信息
		String json = statInfoByType(ltGroup, tpGroup, statFun, statCol, "bj", criteria);
		WebAppUtils.ajaxResponse(response, json);
	}

	/*
	 * 活动统计
	 */
	@RequestMapping(value = "/liftClubStatistics.jhtml")
	public void liftClubStatistics(String ltGroup, String tpGroup, String statFun,
			String statCol, String criteria, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		// --获取活动[hd]的统计信息
		String json = statInfoByType(ltGroup, tpGroup, statFun, statCol, "hd", criteria);
		WebAppUtils.ajaxResponse(response, json);
	}

	/*
	 * 产品统计
	 */
	@RequestMapping(value = "/productStatistics.jhtml")
	public void productStatistics(String ltGroup, String tpGroup, String statFun,
			String statCol, String criteria, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		// --获取产品[cp]的统计信息
		String json = statInfoByType(ltGroup, tpGroup, statFun, statCol, "cp", criteria);
		WebAppUtils.ajaxResponse(response, json);
	}

	/*
	 * 任务统计
	 */
	@RequestMapping(value = "/taskStatistics.jhtml")
	public void taskStatistics(String ltGroup, String tpGroup, String statFun,
			String statCol, String criteria, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		String json = statInfoByType(ltGroup, tpGroup, statFun, statCol, "rw", criteria);
		WebAppUtils.ajaxResponse(response, json);
	}

	/*
	 * 商品统计
	 */
	@RequestMapping(value = "/goodsStatistics.jhtml")
	public void goodsStatistics(String ltGroup, String tpGroup, String statFun,
			String statCol, String criteria, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		initFrontHeader(request, model);
		String json = statInfoByType(ltGroup, tpGroup, statFun, statCol, "sp", criteria);
		WebAppUtils.ajaxResponse(response, json);
	}

	/****************************************************************/
	/**
	 * 信息统计方法
	 * 
	 * @param ltGroup
	 * @param tpGroup
	 * @param statFun
	 * @param statCol
	 * @param statType
	 *            统计类别（会员，班级，活动，产品，商品，任务）
	 * @return
	 */
	public String statInfoByType(String ltGroup, String tpGroup, String statFun,
			String statCol, String statType, String criteria) {
		// --左侧分组条件
		List<String> leftGroupBy = getGroups(ltGroup);

		// --头部分组条件
		List<String> topGroupBy = getGroups(tpGroup);

		// --统计会员结果
		List<Object[]> results = null;
		if ("hy".equals(statType)) {
			results = statisticsService.memberStatQueryByCdts(leftGroupBy, topGroupBy,
					statCol, statFun, criteria);
		} else if ("bj".equals(statType)) {
			results = statisticsService.clazzStatQueryByCdts(leftGroupBy, topGroupBy,
					statCol, statFun, criteria);
		} else if ("hd".equals(statType)) {
			results = statisticsService.liftClubStatQueryByCdts(leftGroupBy, topGroupBy,
					statCol, statFun, criteria);
		} else if ("sp".equals(statType)) {
			results = statisticsService.goodsStatQueryByCdts(leftGroupBy, topGroupBy,
					statCol, statFun, criteria);
		} else if ("cp".equals(statType)) {
			results = statisticsService.productStatQueryByCdts(leftGroupBy, topGroupBy,
					statCol, statFun, criteria);
		} else if ("rw".equals(statType)) {
			results = statisticsService.taskStatQueryByCdts(leftGroupBy, topGroupBy,
					statCol, statFun, criteria);
		}
		GroupByTableBuilder gbc = new GroupByTableBuilder(results, leftGroupBy.size(),
				topGroupBy.size());
		if (results == null || results.isEmpty()) {
			return "";
		}
		TableView tv = gbc.buildTable();
		return JSON.toJSONString(tv);
	}

	// -- 获取组集合
	public List<String> getGroups(String group) {
		List<String> gps = new ArrayList<String>();
		if (StringUtils.isNotBlank(group)) {
			String[] groups = group.split(",");
			gps = Arrays.asList(groups);
		}
		return gps;
	}

	/**
	 * 进入统计查询会籍顾问-工作记录界面
	 * 
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/toKeeperRecordQuery.jhtml")
	public String keeperRecordQuery(HttpServletResponse response,
			HttpServletRequest request, ModelMap model) {
		initFrontHeader(request, model);
		User user = getUser(request);
		String hallCode = user.getOrganizationId();
		// --会籍顾问
		model.put("keepers", userService.findUsersByGroupId("3", hallCode));
		// --会馆名称
		model.put("hallName", hallService.findByCode(hallCode).getName());
		return "statistics_keeper_record";
	}

	/**
	 * 统计查询会籍顾问-工作记录
	 * 
	 * @param hallCode
	 * @param fromDate
	 * @param toDate
	 * @param keeperId
	 * @param statCol
	 * @param statType
	 * @param criteria
	 */
	@RequestMapping(value = "/keeperRecordQuery.jhtml")
	public void keeperRecordQuery(String fromDate, String toDate, String keeperId,
			HttpServletResponse response, HttpServletRequest request) {
		String hallCode = getUser(request).getOrganizationId();
		String json = statisticsService.statKeeperRecordQuery(hallCode, fromDate, toDate,
				keeperId);
		WebAppUtils.ajaxResponse(response, json);
	}
}
